package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_COMPANY_ID;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_FIELD_CODE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_FIELD_NAME;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_FIELD_TYPE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_IS_EDIT;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_IS_NECESSARY;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_IS_OPEN;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_MODULE_CODE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_MODULE_NAME;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.E_SORT;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_COMPANY_ID;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_FIELD_CODE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_FIELD_NAME;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_FIELD_TYPE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_IS_EDIT;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_IS_NECESSARY;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_IS_OPEN;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_MODULE_CODE;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_MODULE_NAME;
import static org.jsola.hr.example.CompanyNecessaryFieldExample.N_SORT;

/**
 * 公司员工必填字段信息
 *
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CompanyNecessaryFieldQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_SORT, example = E_SORT)
    private Integer sort;

    @ApiParam(value = N_IS_EDIT, example = E_IS_EDIT)
    private Integer isEdit;

    @ApiParam(value = N_FIELD_TYPE, example = E_FIELD_TYPE)
    @Length(max = 2)
    private String fieldType;

    @ApiParam(value = N_FIELD_CODE, example = E_FIELD_CODE)
    @Length(max = 32)
    private String fieldCode;

    @ApiParam(value = N_FIELD_NAME, example = E_FIELD_NAME)
    @Length(max = 32)
    private String fieldName;

    @ApiParam(value = N_IS_NECESSARY, example = E_IS_NECESSARY)
    private Boolean isNecessary;

    @ApiParam(value = N_IS_OPEN, example = E_IS_OPEN)
    private Boolean isOpen;

    @ApiParam(value = N_MODULE_NAME, example = E_MODULE_NAME)
    @Length(max = 50)
    private String moduleName;

    @ApiParam(value = N_MODULE_CODE, example = E_MODULE_CODE)
    @Length(max = 30)
    private String moduleCode;

    @ApiParam(value = N_COMPANY_ID, example = E_COMPANY_ID)
    @Length(max = 32)
    private String companyId;

    @ApiParam(value = "员工id")
    private String empInfoId;

    @ApiParam(value = "员工id集合")
    private List<String> empInfoIdList;

    @ApiParam(value = "id(某个模块的某条id)")
    private String id;

    @ApiParam(value = "类型，1个人信息设置，2工资社保设置，3材料附件设置")
    private Integer type;

    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public CompanyNecessaryFieldQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

